//todo
import {cbs} from '../../config/settings.js';

module.exports = {
    name: 'login',
    data() {
        return {
            winSize: {
                width: '',
                height: ''
            },

            formOffset: {
                position: 'absolute',
                left: '',
                top: ''
            },

            // remumber: this.$store.state.user.remumber,

            login_actions: {
                disabled: false
            },

            data: {
                account: '',
                password: ''
            },

            rule_data: {
                account: [{
					validator:(rule, value, callback)=>{
						if (value === '') {
							callback(new Error('请输入用户名'));
						} else {
							if(/^[a-zA-Z0-9_-]{3,16}$/.test(value)){
								callback();
							}else{
								callback(new Error('用户名至少3位,由大小写字母和数字,-,_组成'));
							}
						}
					},
                    trigger: 'blur'
                }],
                password: [{
					validator:(rule, value, callback)=>{
						if (value === '') {
							callback(new Error('请输入密码'));
						} else {
						    if(!(/^[a-zA-Z0-9_-]{6,16}$/.test(value))){
								callback(new Error('密码至少6位,由大小写字母和数字,-,_组成'));
							}else{
								callback();
                            }

						}
                    },
                    trigger: 'blur'
                }],
            }
        }
    },
    methods: {
        setSize() {
            this.winSize.width = $(window).width() + "px";
            this.winSize.height = $(window).height() + "px";

            this.formOffset.left = (parseInt(this.winSize.width) / 2 - 175) + 'px';
            this.formOffset.top = (parseInt(this.winSize.height) / 2 - 178) + 'px';
        },

        onLogin(ref,type) {
            this.$refs[ref].validate((valid) => {
                if(valid) {
                    this.login_actions.disabled = true;
                    let data = this.$$formData(this.data);
                    console.log(data.account)
                    this.$$api_user_login(data,res => {
                        if(res.event === 0) {
                            this.$store.dispatch('update_userinfo',{
                                userinfo:res.obj
                            }).then(() =>{
                                this.login_actions.disabled = false;
                                this.$router.push('/product');
                            });
                        }else if(res.event === 1) {

                        }
                    })
                }
            })
        },

        onReminder() {
            this.$router.push('/reminder');
        },


    },
    created() {
        this.setSize();
        $(window).resize(() => {
            this.setSize();
        });
    },
    mounted() {
        // this.toggleStatus(true);
        // console.log(this.remumber);

        //如果上次登录选择的是记住密码并登录成功，则会保存状态，用户名以及token
        // if (this.remumber.remumber_flag === true) {
        //     this.data.username = this.remumber.remumber_login_info.username;
        //     this.data.password = this.remumber.remumber_login_info.token.substring(0, 16);
        //     this.$set(this.data, 'token', this.remumber.remumber_login_info.token);
        // }
    }
}